package com.hafu.dao;

import com.hafu.model.RuKuMain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RuKuMainDao {
    public int addRuKuMain(RuKuMain ruKumain, Connection conn) throws SQLException
    {
        String sql = "insert into ruku_main (ruku_name,price,gys_name,rk_date) value(?,?,?,current_timestamp)";
        PreparedStatement preparedStatement = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1, ruKumain.getRukuname());
        preparedStatement.setBigDecimal(2, ruKumain.getPrice());
        preparedStatement.setString(3, ruKumain.getGysname());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            generatedKeys.next();
            return generatedKeys.getInt(1);
        }else {
            return -1;
        }
    }
}

